package com.ruoyi.framework.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.vo.SysConfigVO;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqIpBlack;
import com.ruoyi.wq.service.IWqIpBlackService;

import cn.hutool.core.date.DateUtil;

@Component
public class AppConfigService {

	@Autowired
	private ISysConfigService sysConfigService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
	private IWqIpBlackService wqIpBlackService;
	
	public String getOpenValue(String appId) {
		SysConfigVO config = new SysConfigVO();
		config.setAppId(appId);
		return sysConfigService.getChatValue(config);
	}
	
	public String getChatValue(SysConfigVO vo) {
		QueryWrapper<SysConfig> query = new QueryWrapper<>();
		SysConfig entity = new SysConfig();
		entity.setConfigKey("chat_open:"+vo.getAppId());
		query.setEntity(entity);
		SysConfig config = sysConfigService.getOne(query);
		// 审核期间
		if(vo.getUserId() == null || vo.getUserId().intValue() == 0){
			if("wx2b5596fc2c4454d4".equals(vo.getAppId())){
				return "0";
			}
			
			String loginIp = vo.getScene();
			if(StringUtils.isNotBlank(loginIp)){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(loginIp);
	        	if(ipBlack != null){
	        		if("0".equals(config.getConfigValue())){
	        			return "1";
	        		}else{
	        			return config.getConfigValue();
	        		}
	        	}else{
	        		return "0";
	        	}
	        }else{
	        	return "1";
	        }
			
		}else{
			SysUser user = sysUserService.getById(vo.getUserId());
			
			if(StringUtils.isNotBlank(user.getScene())){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(user.getScene());
	        	if(ipBlack != null){
	        		user.setIsAuth("1");
	        	}
	        }
			
			if(user.getVisitUserId() != null){
				user.setIsAuth("1");
			}
			if(StringUtils.isNotBlank(user.getWxOpenId())){
				user.setIsAuth("1");
			}
			if(user.getPayNum() >= 10){
				user.setIsAuth("1");
			}else{
				if("wx2b5596fc2c4454d4".equals(user.getAppId())){
					user.setIsAuth("0");
				}
			}
			return user.getIsAuth();
		}
	}
	
	public String getPlayValue(SysConfigVO vo) {
		QueryWrapper<SysConfig> query = new QueryWrapper<>();
		SysConfig entity = new SysConfig();
		entity.setConfigKey("chat_open:"+vo.getAppId());
		query.setEntity(entity);
		SysConfig config = sysConfigService.getOne(query);
		// 审核期间
		if(vo.getUserId() == null || vo.getUserId().intValue() == 0){
			String loginIp = IpUtils.getIpAddr(ServletUtils.getRequest());
			if(StringUtils.isNotBlank(loginIp)){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(loginIp);
	        	if(ipBlack == null){
	        		if(vo.getShareUserId() == null){
	        			return config.getConfigValue();
	        		}else{
	        			return "1";
	        		}
	        	}else{
	        		return "0";
	        	}
	        }else{
	        	return "0";
	        }
		}else{
			SysUser user = sysUserService.getById(vo.getUserId());
			
			if(StringUtils.isNotBlank(user.getLoginIp())){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(user.getLoginIp());
	        	if(ipBlack == null){
	        		user.setIsAuth("1");
	        	}
	        }
			
			if(user.getVisitUserId() != null){
				user.setIsAuth("1");
			}
			if(StringUtils.isNotBlank(user.getPhonenumber())){
				user.setIsAuth("1");
			}
			if(StringUtils.isNotBlank(user.getWxOpenId())){
				user.setIsAuth("1");
			}
			if(user.getPayNum() > 10){
				user.setIsAuth("1");
			}
			return user.getIsAuth();
		}
	}

	public String getChatConfig(SysConfigVO vo) {
		QueryWrapper<SysConfig> query = new QueryWrapper<>();
		SysConfig entity = new SysConfig();
		entity.setConfigKey("chat_open:"+vo.getAppId());
		query.setEntity(entity);
		SysConfig config = sysConfigService.getOne(query);
		// 审核期间
		if(vo.getUserId() == null || vo.getUserId().intValue() == 0){
			String loginIp = vo.getScene();
			if(StringUtils.isNotBlank(loginIp)){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(loginIp);
	        	if(ipBlack != null){
	        		if("0".equals(config.getConfigValue())){
	        			return "2";
	        		}else{
	        			return config.getConfigValue();
	        		}
	        	}else{
	        		return "0";
	        	}
	        }else{
	        	return "2";
	        }
		}else{
			SysUser user = sysUserService.getById(vo.getUserId());
			
			if(StringUtils.isNotBlank(user.getScene())){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(user.getScene());
	        	if(ipBlack != null){
	        		user.setIsAuth("2");
	        	}
	        }else{
	        	user.setIsAuth("2");
	        }
			
			if(user.getVisitUserId() != null){
				user.setIsAuth("2");
			}
			if(StringUtils.isNotBlank(user.getPhonenumber())){
				user.setIsAuth("2");
			}
			if(StringUtils.isNotBlank(user.getWxOpenId())){
				user.setIsAuth("2");
			}
			return user.getIsAuth();
		}
	}

	public String getMenuValue(SysConfigVO vo) {
		// 审核期间
		if(vo.getUserId() == null || vo.getUserId().intValue() == 0){
			if(StringUtils.isNotBlank(vo.getScene())){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(vo.getScene());
	        	if(ipBlack == null){
	        		return "0";
	        	}else{
	        		return "1";
	        	}
	        }else{
	        	return "0";
	        }
		}else{
			SysUser user = sysUserService.getById(vo.getUserId());
			
			if(StringUtils.isNotBlank(user.getScene())){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(user.getScene());
	        	if(ipBlack != null){
	        		user.setIsAuth("1");
	        	}
	        }
			
			if(user.getVisitUserId() != null){
				user.setIsAuth("1");
			}
			if(StringUtils.isNotBlank(user.getPhonenumber())){
				user.setIsAuth("1");
			}
			if(StringUtils.isNotBlank(user.getWxOpenId())){
				user.setIsAuth("1");
			}
			if(user.getPayNum() >= 10){
				user.setIsAuth("1");
			}
			return user.getIsAuth();
		}
	}

	public String getCardValue(SysConfigVO vo) {
		// 0 表示审核  1表示用户 2表示已实名用户
		String auth = "0";
		if(vo.getUserId() == null){
			if(StringUtils.isNotBlank(vo.getScene())){
	        	WqIpBlack ipBlack = wqIpBlackService.getIpBlack(vo.getScene());
	        	if(ipBlack != null){
	        		auth = "1";
	        	}
	        }
		}else{
			SysUser user = sysUserService.getById(vo.getUserId());
			if(user != null){
				if(StringUtils.isNotBlank(user.getScene())){
					WqIpBlack ipBlack = wqIpBlackService.getIpBlack(user.getScene());
		        	if(ipBlack != null){
		        		auth = "1";
		        	}
		        	
		        	if("1".equals(user.getIsAuth())){
		        		auth = "1";
		        	}
		        	
		        	if(user.getVisitUserId() != null){
		        		auth = "1";
		        	}
		        	
		        	if(StringUtils.isNotBlank(user.getWxOpenId())){
		        		auth = "1";
		        	}
					
					if(user.getPayNum() >= 10){
						auth = "2";
					}
					
					// 放开权限
					if(ipBlack != null){
		        		auth = "2";
		        	}
		        	
		        	if("1".equals(user.getIsAuth())){
		        		auth = "2";
		        	}
		        	
		        	if(user.getVisitUserId() != null){
		        		auth = "2";
		        	}
		        	
		        	if(StringUtils.isNotBlank(user.getWxOpenId())){
		        		auth = "2";
		        	}
					
					if(user.getPayNum() >= 10){
						auth = "2";
					}
					
				}
			}
		}
		return auth;
	}

	public String getMenuAuthValue(SysConfigVO vo) {
		// 审核期间
		if(vo.getUserId() == null){
			return "0";
		}else{
			SysUser user = sysUserService.getById(vo.getUserId());
			if(user == null){
				return "0";
			}else{
				if(StringUtils.isNotBlank(vo.getIsAuth())){
					if("0".equals(user.getIsAuth())){
						user.setIsAuth(vo.getIsAuth());
						sysUserService.updateById(user);
					}
				}
				if(user.getPayNum() >= 10){
					user.setIsAuth("1");
				}
				
				if(user.getVisitUserId() != null){
					user.setIsAuth("1");
				}
				
				if(StringUtils.isNotBlank(user.getWxOpenId())){
					user.setIsAuth("1");
        		}
        		
        		if(StringUtils.isNotBlank(user.getScene())){
        			WqIpBlack ipBlack = wqIpBlackService.getIpBlack(user.getScene());
    	        	if(ipBlack != null){
    	        		user.setIsAuth("1");
    	        	}
        		}
				
				return user.getIsAuth();
			}
		}
	}
	
	public static void main(String[] args) {
		System.out.println(DateUtil.thisHour(true));
	}
}
